Using Classes
A class definition serves as a template from which we create objects for the use of our application code. For example, by using our class definition of Point, we can create several Point objects. Each Point object will be characterized by two distinct fields, x and y, which specify the x-coordinate and y-coordinate of the point, respectively. Each Point object will have its own copy of the data members x and y.
class Point
{
int x;
int y;
}
These members are called instance variables of an object because they belong to a particular instance of the class. The process of creating an object from a class definition is called class instantiation. We say that a class has been instantiated when we create an object.
To
create an object, we use the following declaration:
Point
p = new Point();
We
use the new keyword to instantiate a class. We specify the class name
after the new keyword, followed by opening and closing parentheses. The opening
and closing parentheses indicate a method call—a call to a class constructor. Once a
class is instantiated, memory will be allocated for the object of the class to
hold its data. The reference to this memory allocation must be copied and saved
somewhere so that the created object can be accessed at a later time in our
program code. In the preceding statement, we copy this memory reference to the
variable p declared on the left-hand side of the assignment operator. The type
of variable p is Point, indicating that p holds a reference to a Point-type
object. When the preceding program statement is executed, an object of type
Point is created at runtime. This Point object contains two fields: x and y.
The object will be referred to by the variable p later in the program. The two
fields, x and y, take a default integer value of 0. The default value assigned
to a field depends on its type.
Accessing/Modifying Fields
Accessing
the fields declared in the point class is simple. To access the x-coordinate of
the point p, we use the syntax p.x, and to access the y-coordinate,
we use the syntax p.y. The general form for accessing a field is:
objectReference.fieldName.
With
respect to our example, objectReference is p
and fieldname is y or x.
The Class Example Program
We’ll
now write a Java program instantiate our
point class and use it in the application. Here full program for Test class for
using a Point class.
class TestPoint {
public static void main(String[] args) {
System.out.println("Creating a Point object ... ");
Point p = new Point();
System.out.println("Initializing data members ...");
p.x = 4;
p.y = 5;
System.out.println("Printing object");
System.out.println("Point p (" + p.x + ", " + p.y + ")");
}
}
Output:
Creating a Point object ...
Initializing data members ...
Printing object
Point p (4, 5)
To
test the Point class, we need to write another class. The preceding program
defines this other class, called TestPoint. The TestPoint class declares a main
method where the program execution begins. In the main method body, we create
an instance of the Point class using the following program statement
Point
p = new Point();
We
access the fields x and y of the created object using following statements:
p.x
= 4;
p.y
= 5;
These
statements set the values of the two data members (that is, the fields). As
stated earlier, these members are also called instance variables because they
belong to a particular instance of the class; in this case, the instance is p.
If we create another instance (say, p2), it will have its own copy of the x and
y fields. Here, we use the syntax objectReference.fieldName to access
a field.
The
two statements assign values to the two fields. We verify this assignment by
printing the object contents in the next two lines of the program code:
System.out.println("Printing
object");
System.out.println("Point
p (" + p.x + ", " + p.y + ")");
Here,
we use the same syntax, p.x and p.y, as in the earlier case for retrieving the
instance variables.
Simond Gear
30-Mar-2017